# @Version        : 1.0
# @Update Time    : 2025/9/19 22:06
# @File           : event.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息
from blinker import signal

# 登录事件
do_login_event = signal("do_login_event")
# 登出事件
do_logout_event = signal("do_logout_event")
# 踢出事件
do_kickout_event = signal("do_kickout_event")
# 登录顶替事件
do_replaced_event = signal("do_replaced_event")
# 登录超时事件
do_renew_timeout_event = signal("do_renew_timeout_event")
# 登录禁用事件
do_disable_event = signal("do_disable_event")
# 登录解禁事件
do_untie_disable_event = signal("do_untie_disable_event")
# 登录安全事件
do_open_safe_event = signal("do_open_safe_event")
# 二级认证事件
do_close_safe_event = signal("do_close_safe_event")


@do_login_event.connect
def do_login(sender, login_type, login_id, token_value, login_parameter):
    print(login_type, login_id, token_value, login_parameter)
    print(f"用户 {login_id} 登录成功，Token: {token_value}")


@do_logout_event.connect
def do_logout(sender, login_type, login_id, token_value):
    print(f"用户类型为：{login_type},用户id: {login_id}, token: {token_value} 退出登录")


@do_kickout_event.connect
def do_kickout(sender, login_type, login_id, token_value):
    print(f"用户类型为：{login_type},用户id: {login_id}, token: {token_value} 剔除下线")


@do_replaced_event.connect
def do_replaced(sender, login_type, login_id, token_value):
    print(f"用户类型为：{login_type},用户id: {login_id}, token: {token_value} 顶踢下线")


@do_renew_timeout_event.connect
def do_renew_timeout(sender, login_type, login_id, token_value, timeout):
    print(f"用户 {login_id} 的Token已续期，新超时时间: {timeout}")


@do_disable_event.connect
def do_disable(sender, login_type, login_id, service, level, time):
    print(f"用户 {login_id} 的服务 {service} 被封禁，等级: {level}，时间: {time}")


@do_untie_disable_event.connect
def do_untie_disable(sender, login_type, login_id, service):
    print(f"用户 {login_id} 的服务 {service} 已解封")


@do_open_safe_event.connect
def do_open_safe(sender, login_type, token_value, service, safe_time):
    print(f"Token {token_value} 开启了二级认证，服务: {service}，安全时间: {safe_time}")


@do_close_safe_event.connect
def do_close_safe(sender, login_type, token_value, service):
    print(f"Token {token_value} 关闭了二级认证，服务: {service}")


# pip install lato
from typing import Any, Optional, Union
from lato import Application, ApplicationModule, Event


# 1) 定义事件（把 blinker 的 signal + 参数组合，映射为具体的 Event 类型）
class DoLoginEvent(Event):
    login_type: str
    login_id: str
    token_value: str
    login_parameter: Any
    sender: Optional[str] = None


class DoLogoutEvent(Event):
    login_type: str
    login_id: str
    token_value: str
    sender: Optional[str] = None


class DoKickoutEvent(Event):
    login_type: str
    login_id: str
    token_value: str
    sender: Optional[str] = None


class DoReplacedEvent(Event):
    login_type: str
    login_id: str
    token_value: str
    sender: Optional[str] = None


class DoRenewTimeoutEvent(Event):
    login_type: str
    login_id: str
    token_value: str
    timeout: Union[int, float]
    sender: Optional[str] = None


class DoDisableEvent(Event):
    login_type: str
    login_id: str
    service: str
    level: Union[int, str]
    time: Union[int, float]
    sender: Optional[str] = None


class DoUntieDisableEvent(Event):
    login_type: str
    login_id: str
    service: str
    sender: Optional[str] = None


class DoOpenSafeEvent(Event):
    login_type: str
    token_value: str
    service: str
    safe_time: Union[int, float]
    sender: Optional[str] = None


class DoCloseSafeEvent(Event):
    login_type: str
    token_value: str
    service: str
    sender: Optional[str] = None


# 2) 定义模块与事件处理器（等价于 blinker 的 @signal.connect）
auth_module = ApplicationModule(name="auth")


@auth_module.handler(DoLoginEvent)
def on_login(e: DoLoginEvent):
    print(e.login_type, e.login_id, e.token_value, e.login_parameter)
    print(f"用户 {e.login_id} 登录成功，Token: {e.token_value}")


@auth_module.handler(DoLogoutEvent)
def on_logout(e: DoLogoutEvent):
    print(
        f"用户类型为：{e.login_type},用户id: {e.login_id}, token: {e.token_value} 退出登录"
    )


@auth_module.handler(DoKickoutEvent)
def on_kickout(e: DoKickoutEvent):
    print(
        f"用户类型为：{e.login_type},用户id: {e.login_id}, token: {e.token_value} 剔除下线"
    )


@auth_module.handler(DoReplacedEvent)
def on_replaced(e: DoReplacedEvent):
    print(
        f"用户类型为：{e.login_type},用户id: {e.login_id}, token: {e.token_value} 顶踢下线"
    )


@auth_module.handler(DoRenewTimeoutEvent)
def on_renew_timeout(e: DoRenewTimeoutEvent):
    print(f"用户 {e.login_id} 的Token已续期，新超时时间: {e.timeout}")


@auth_module.handler(DoDisableEvent)
def on_disable(e: DoDisableEvent):
    print(
        f"用户 {e.login_id} 的服务 {e.service} 被封禁，等级: {e.level}，时间: {e.time}"
    )


@auth_module.handler(DoUntieDisableEvent)
def on_untie_disable(e: DoUntieDisableEvent):
    print(f"用户 {e.login_id} 的服务 {e.service} 已解封")


@auth_module.handler(DoOpenSafeEvent)
def on_open_safe(e: DoOpenSafeEvent):
    print(
        f"Token {e.token_value} 开启了二级认证，服务: {e.service}，安全时间: {e.safe_time}"
    )


@auth_module.handler(DoCloseSafeEvent)
def on_close_safe(e: DoCloseSafeEvent):
    print(f"Token {e.token_value} 关闭了二级认证，服务: {e.service}")


# 3) 组装应用
app = Application()
app.include_submodule(auth_module)

# 4) 触发事件（等价于 blinker 的 signal.send(...)）
if __name__ == "__main__":
    app.publish(
        DoLoginEvent(
            login_type="user",
            login_id="u1",
            token_value="tok123",
            login_parameter={"ip": "1.2.3.4"},
        )
    )
    # app.publish(DoLogoutEvent(login_type="user", login_id="u1", token_value="tok123"))
    # app.publish(DoKickoutEvent(login_type="user", login_id="u1", token_value="tok123"))
    # app.publish(DoReplacedEvent(login_type="user", login_id="u1", token_value="tok123"))
    # app.publish(DoRenewTimeoutEvent(login_type="user", login_id="u1", token_value="tok123", timeout=3600))
    # app.publish(DoDisableEvent(login_type="user", login_id="u1", service="chat", level=2, time=7200))
    # app.publish(DoUntieDisableEvent(login_type="user", login_id="u1", service="chat"))
    # app.publish(DoOpenSafeEvent(login_type="user", token_value="tok123", service="transfer", safe_time=900))
    # app.publish(DoCloseSafeEvent(login_type="user", token_value="tok123", service="transfer"))


# if __name__ == "__main__":
#     do_logout_event.send(
#         "do_logout_event",
#         login_type="login_type",
#         login_id="login_id",
#         token_value="token_value",
#     )
